package com.my.chapter15;

import net.mindview.util.Generator;

import java.util.*;

public class practice41 {
    public static void main(String[] args) {
        List<SPet> carrier = new ArrayList<SPet>();
        Fill.fill(
                new AddableCollectionAdapter<SPet>(carrier),
                SPet.class, 3);
        Fill.fill(Adapter.collectionAdapter(carrier),
                SMouse.class, 2);
        for(SPet p: carrier)
            System.out.println(p);
        System.out.println("----------------------");
        AddableSimpleQueue<SPet> petQueue =
                new AddableSimpleQueue<SPet>();
        Fill.fill(petQueue, SMutt.class, 2);
        Fill.fill(petQueue, SHamster.class, 3);
        for(SPet p: petQueue)
            System.out.println(p);
    }
}

interface Addable<T>{ void add(T t); }

class Fill{
    public static <T> void fill(Addable<T> addable,Class<? extends T> classToken,int size){
        for(int i=0;i<size;i++){
            try {
                addable.add(classToken.newInstance());
            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }
    }

    public static <T> void fill(Addable<T> addable, Generator<T> generator, int size){
        for(int i=0;i<size;i++){
            try {
                addable.add(generator.next());
            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }
    }
}

class AddableCollectionAdapter<T> implements Addable<T>{
    private Collection<T> c;

    public AddableCollectionAdapter(Collection<T> c){
        this.c = c;
    }

    @Override
    public void add(T t) {
        c.add(t);
    }
}

class Adapter{
    public static <T> Addable<T> collectionAdapter(Collection<T> c){
        return new AddableCollectionAdapter<>(c);
    }
}

class SimpleQueue<T> implements Iterable<T>{
    private LinkedList<T> storage = new LinkedList<T>();

    public void add(T t){
        storage.offer(t);
    }

    public T get(){
        return storage.poll();
    }

    @Override
    public Iterator<T> iterator() {
        return storage.iterator();
    }
}

class AddableSimpleQueue<T> extends SimpleQueue<T> implements Addable<T>{
    public void add(T item){
        super.add(item);
    }
}


